<?php

namespace App\Services\Stats\Risk\Slo\Elasticsearch;

use App\Services\Service;
use App\Services\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info as SettlementInfo;
use App\Models\Admin\Stats\Risk\Config;
use App\Core\Traits\Numeral;

class OrgGameService extends Service
{
    use DatabaseElasticsearch, Numeral;

    /**
     * 制作统计参数
     *
     * @param \Illuminate\Http\Request $request
     * @param array $cresset
     * @return array
     */
    private function makeAggParams($request, $cresset)
    {
        return [
            'body' => [
                'size' => 0,
                'aggs' => [
                    'group_stats' => [
                        'terms' => [
                            'script' => "doc['plat_code'].value + '.' + doc['line_code'].value + '.' + doc['game_id'].value",
                            'size' => 10000,
                        ],
                        'aggs' => [
                            'group_bucket_sort' => [
                                'bucket_sort' => [
                                    'size' => $this->getPageSize($request),
                                    'sort' => [
                                        [
                                            'gain_player' => [
                                                'order' => 'desc'
                                            ],
                                            'rtp' => [
                                                'order' => 'desc'
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            'bet_num' => [
                                'sum' => [
                                    'field' => 'usd.bet_num'
                                ]
                            ],
                            'gain_player' => [
                                'sum' => [
                                    'field' => 'usd.gain_player'
                                ]
                            ],
                            'rtp' => [
                                'bucket_script' => [
                                    'buckets_path' => [
                                        'bet_num' => 'bet_num',
                                        'gain_player' => 'gain_player'
                                    ],
                                    'script' => '1 + params.gain_player / params.bet_num'
                                ]
                            ],
                            'cresset_bucket_filter' => [
                                'bucket_selector' => [
                                    'buckets_path' => [
                                        'rtp' => 'rtp',
                                        'gain_player' => 'gain_player'
                                    ],
                                    'script' => "params.rtp {$cresset['rtp'][0]} {$cresset['rtp'][1]} && params.gain_player {$cresset['gain_player'][0]} {$cresset['gain_player'][1]}"
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];
    }

    /**
     * 获取排行数据
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function getTopList($request)
    {
        // 灯号配置
        $cresset = Config::getCofing(Config::TYPE_SLO)['orgGame'];

        // 制作统计参数
        $params = $this->makeAggParams($request, end($cresset));

        // 筛选【平台】
        $this->makeWhereParamOrg($request, $params);
        // 筛选【平台】【排除】
        $this->makeWhereParamNot($request, 'plat_code_exclude', $params, 'plat_code');
        // 筛选【游戏】
        $this->makeWhereParam($request, 'game_id', $params);
        // 筛选【游戏】【排除】
        $this->makeWhereParamNot($request, 'game_id_exclude', $params, 'game_id');
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $params, null, 'datetime', SettlementInfo::INDEX_NAME);
        // 筛选【搜索】
        $this->makeWhereParamSearch($request, $params);
        // 筛选【slo】【免费游戏】
        $this->makeWhereParam($request, 'ext.is_free', $params);
        // 筛选【slo】【小游戏】
        $this->makeWhereParam($request, 'ext.is_bonus', $params);

        // 查询数据
        $results = SettlementInfo::search($params);

        // 制作数据
        $datas = [];
        foreach ($results['aggregations']['group_stats']['buckets'] ?? [] as $item) {
            list ($platCode, $lineCode, $gameId) = explode('.', $item['key']);
            // 游戏统计
            array_push($datas, [
                'plat_code' => $platCode,
                'line_code' => $lineCode,
                'game_id' => $gameId,
                'doc_count' => $item['doc_count'],
                'bet_num' => $this->makeRound($item['bet_num']['value']),
                'gain_player' => $this->makeRound($item['gain_player']['value']),
                'rtp' => $this->makeRound($item['rtp']['value']),
            ]);
        }

        return [
            $datas,
            $cresset,
        ];
    }
}
